package logic

import (
	"context"
	"encoding/json"
	"github.com/olivere/elastic/v7"
	"server/models"
	"server/proto"
)

func (s *ServerRpc) InquireFruit(ctx context.Context, in *proto.InquireFruitReq) (*proto.InquireFruitRes, error) {
	var rr proto.InquireFruitRes
	rr.Data = nil
	high := elastic.NewHighlight()
	text := elastic.NewHighlighterField("name")
	text.NumOfFragments(0)
	text.PreTags("<i>").PostTags("</i>")
	high.Fields(text)
	data, err := models.InquireEs(in.Index, high, elastic.NewMultiMatchQuery(in.Word, "name", "title", "content"), int(in.Page), int(in.Size))
	if err != nil {
		rr.Data = nil
		return &rr, err
	}
	res, _ := json.Marshal(data)
	rr.Data = res
	return &rr, nil
}
